<template>
  <div>
    <el-dialog v-model="state.showDialog" destroy-on-close :title="title" draggable :close-on-click-modal="false"
      :close-on-press-escape="false" width="569px">
      <el-form ref="formRef" :model="form" size="default" label-width="80px">
        <el-row :gutter="35">
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="产品代号" prop="productCode"
              :rules="[{ required: true, message: '请输入产品代号', trigger: ['blur', 'change'] }]">
              <el-input v-model="form.productCode" placeholder="请输入产品代号" autocomplete="off" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="产品名称" prop="productName"
              :rules="[{ required: true, message: '请输入产品名称', trigger: ['blur', 'change'] }]">
              <el-input v-model="form.productName" placeholder="请输入产品名称" autocomplete="off" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="排序" prop="sort"
              :rules="[{ required: false, message: '排序', trigger: ['blur', 'change'] }]">
              <el-input-number v-model="form.sort" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="设备配方" :rules="[{ required: true, message: '请选中设备配方', trigger: ['blur', 'change'] }]">
              <el-select v-model="form.formulaId" placeholder="请选中设备配方">
                <el-option v-for="item in state.DeviceFormulaListData" :key="item.id" :label="item.formulaName"
                  :value="item.id">
                  <span style="float: left">{{ item.formulaName }}</span>
                  <span style="
                              float: right;
                              color: var(--el-text-color-secondary);
                              font-size: 13px;">
                    {{ item.formulaOrder }}
                  </span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="工艺路线" :rules="[{ required: true, message: '请选中工艺路线', trigger: ['blur', 'change'] }]">
              <el-select v-model="form.productRouteId" placeholder="请选中工艺路线">
                <el-option v-for="item in state.ProductRouteListData" :key="item.id" :label="item.routeName"
                  :value="item.id">
                  <span style="float: left">{{ item.routeName }}</span>
                  <span style="
                              float: right;
                              color: var(--el-text-color-secondary);
                              font-size: 13px;">
                    {{ item.routeOrder }}
                  </span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="onCancel" size="default">取 消</el-button>
          <el-button type="primary" @click="onSure" size="default" :loading="state.sureLoading">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup lang="ts" name="industry/product-model/form">
import { reactive, toRefs, getCurrentInstance, ref } from 'vue'
import { ProductModelGetListOutput, ProductModelAddInput, ProductModelUpdateInput, DeviceFormulaGetListOutput, ProductRouteGetListOutput } from '/@/api/industry/data-contracts'
import { ProductModelApi } from '/@/api/industry/ProductModel'
import { DeviceFormulaApi } from '/@/api/industry/DeviceFormula'
import { ProductRouteApi } from '/@/api/industry/ProductRoute'
import eventBus from '/@/utils/mitt'

import { listToTree } from '/@/utils/tree'


defineProps({
  title: {
    type: String,
    default: '',
  },
})
const { proxy } = getCurrentInstance() as any
const formRef = ref()
const state = reactive({
  showDialog: false,
  sureLoading: false,
  ProductionModelData: [] as ProductModelGetListOutput[],
  DeviceFormulaListData: [] as DeviceFormulaGetListOutput[],
  ProductRouteListData: [] as ProductRouteGetListOutput[],
  form: {} as ProductModelAddInput & ProductModelUpdateInput,
})
const { form } = toRefs(state)

//获取设备配方列表
const getDeviceFormulaListData = async () => {
  const res = await new DeviceFormulaApi().getList({})
  if (res?.success && res.data && res.data.length > 0) {
    state.DeviceFormulaListData = listToTree(res.data) as DeviceFormulaGetListOutput[]
  } else {
    state.DeviceFormulaListData = []
  }
}

//获取设备配方列表
const getProductRouteListData = async () => {
  const res = await new ProductRouteApi().getList({})
  if (res?.success && res.data && res.data.length > 0) {
    state.ProductRouteListData = listToTree(res.data) as ProductRouteGetListOutput[]
  } else {
    state.ProductRouteListData = []
  }
}

// 打开对话框
const open = async (row: any = {}) => {
  await getProductRouteListData();
  await getDeviceFormulaListData();
  if (row.id > 0) {
    const res = await new ProductModelApi().get({ id: row.id }, { loading: true }).catch(() => {
      proxy.$modal.closeLoading()
    })

    if (res?.success) {
      state.form = res.data as ProductModelAddInput & ProductModelUpdateInput
    }
  } else {
    state.form = { productSeriesId: row.productSeriesId } as ProductModelAddInput & ProductModelUpdateInput
  }
  state.showDialog = true
}
// 确定
const onSure = () => {
  formRef.value.validate(async (valid: boolean) => {
    if (!valid) return

    state.sureLoading = true
    let res = {} as any
    if (state.form.id != undefined && state.form.id > 0) {
      res = await new ProductModelApi().update(state.form, { showSuccessMessage: true }).catch(() => {
        state.sureLoading = false
      })
    } else {
      res = await new ProductModelApi().add(state.form, { showSuccessMessage: true }).catch(() => {
        state.sureLoading = false
      })
    }
    state.sureLoading = false

    if (res?.success) {
      eventBus.emit('refreshProductModel')
      state.showDialog = false
    }
  })
}

// 取消
const onCancel = () => {
  state.showDialog = false
}
defineExpose({
  open,
})
</script>